/*
	Temperature.h
	Christopher Raffaele
	04.28.2008

	Temperature's class definitions
*/
#include"TemperatureConverter.h" //Temperature class declarations
using namespace std;

//show menu
void TemperatureConverter::showMenu(){
	std::cout << "\n--------------------------------\n"
		 << "\\           Temperature         \\\n"
		 << " ---------------------------------\n"
		 << " | 1 | Centigrade                |\n"
		 << " ---------------------------------\n"
		 << " | 2 | Fahrenheit                |\n"
		 << " ---------------------------------\n";
}

double TemperatureConverter::calculateTemperatureConversion(double fromChoice, double temperature){
	if(fromChoice == 1){
		return temperature * (9.0/5.0) + 32.0;
	}

	return (temperature - 32.0) * (5.0/9.0);
}

//create the units array
TemperatureConverter::TemperatureConverter(){
	units = new string[2];

	createUnitsString();
}

TemperatureConverter::~TemperatureConverter(){
	delete [] units;
}

//set the value in the units array
void TemperatureConverter::createUnitsString(){
	units[0] = "centigrade(c)";
	units[1] = "fahrenheit(f)";
}

//checks if the user wants to continue converting temperatures
bool TemperatureConverter::userWantsToRunAgain(){
	cout << "\nCalculate more temperature conversions(y/n)? ";

	cin.ignore(1000, '\n');

	//check only the first char
	return (cin.get() == 'y');
}

//converts a temperature
void TemperatureConverter::run(){
	showMenu();

	unsigned int convertFrom = getConvertFromToChoice(true);
	unsigned int convertTo = getConvertFromToChoice(true);

	double temperatureToConvert = getTemperatureToConvert();

	//print answer
	if(convertFrom != convertTo){
		cout << "\n" << temperatureToConvert << " " << units[convertFrom -1].c_str() << " is equal to "
		 << calculateTemperatureConversion(convertFrom, temperatureToConvert)
		 << " " << units[convertTo - 1].c_str();
	}
	else{
		cout << "\n" << temperatureToConvert << " " << units[convertFrom -1].c_str() << " is equal to "
		 << temperatureToConvert << " " << units[convertTo - 1].c_str();
	}
}

//gets the units to convert to and from
unsigned int TemperatureConverter::getConvertFromToChoice(bool from){
	//determine which message to show
	if(from)
		cout << "\nWhat units would you like to convert from? ";
	else
		cout << "\nWhat units would you like to convert to? ";

	unsigned int userChoice = 0;

	if(cin.width() != 0) cin.ignore(1000, '\n');

	//input validation
	do{
		cin >> userChoice;

		//invalid input
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000, '\n');
			userChoice = 0;
		}
	//input must be 1 or 2
	}while(userChoice < 1 || userChoice > 2);

	return userChoice;
}

//gets the temperature the user wishes to convert
double TemperatureConverter::getTemperatureToConvert(){
	cout << "\nEnter the temperature you would like to convert: ";

	double temperature = 0;

	if(cin.width() != 0) cin.ignore(1000, '\n');

	cin >> temperature;

	//invalid input
	if(cin.fail()){
		cin.clear();
		cin.ignore(1000, '\n');
		temperature = 0;
	}

	return temperature;
}

//runs temperatur conversions until the user wants to quit
void TemperatureConverter::start(){
	do{
		run();
	}while(userWantsToRunAgain());
}